import { Component, ViewChild, ElementRef } from '@angular/core'
import { IonicPage, NavController, NavParams, Slides } from 'ionic-angular'
import { Storage } from '@ionic/storage'
import { HTTP } from '@ionic-native/http'
import { ConfigsProvider } from '../../providers/configs/configs'
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'
import { HttpServicesProvider } from '../../providers/http-services/http-services'

import { InProductionPage } from '../bizs/in-production/in-production'
import { CompletionPage } from '../bizs/completion/completion'

import { OrderPage } from '../bizs/order/order'
declare let Swiper
@IonicPage()
@Component({
  selector: 'page-production',
  templateUrl: 'production.html'
})
export class ProductionPage {
  swiper: any
  private slidesHeight: number
  private slidesMoving: boolean
  private screenheight: number = screen.height
  private headerheight: number
  private footerheight: number
  @ViewChild('contentSlides')
  contentSlides: Slides
  customer: any
  orderNumber: any
  menus: Array<string> = ['计划任务', '临时任务']
  number: Array<number> = [0, 0]
  scheduledTasks: any[] // 计划任务
  temporaryAssignment: any[] // 临时任务

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private httpService: HttpServicesProvider,
    public element: ElementRef,
    public local_storage: Storage
  ) {
    local_storage
      .get('user')
      .then(value => {
        if (value) {
          let data = {
            inspectionPrincipal: value['userId'],
            limit: 10,
            page: 1
          }
          this.httpService.requestDataGet(
            'app/inspectionwork/findtaskinfobyuser',
            data,
            {},
            (status, datas) => {
              if (status) {
                if (datas['periodicTask']) {
                  this.scheduledTasks = datas['periodicTask']
                  this.number[0] = datas['periodicTask'].length
                }
                if (datas['temporaryTask']) {
                  this.temporaryAssignment = datas['temporaryTask']
                  this.number[1] = datas['temporaryTask'].length
                }
              }
            }
          )
          this.scheduledTasks = [
            { inspectionDate: '2018.10.30 11:11', inspectionWorkName: '测试' }
          ]
          return
        }
      })
      .catch(err => {
        console.error(err)
      })
  }

  ionViewDidLoad() {
    this.initSwiper()
    this.headerheight = this.element.nativeElement.firstChild.clientHeight
    this.footerheight = this.element.nativeElement.parentNode.parentNode.firstChild.clientHeight
  }

  initSwiper() {
    this.swiper = new Swiper('.pageMenuSlides .swiper-container', {
      slidesPerView: 2,
      spaceBetween: 0,
      breakpoints: {
        1024: {
          slidesPerView: 2,
          spaceBetween: 0
        },
        768: {
          slidesPerView: 2,
          spaceBetween: 0
        },
        640: {
          slidesPerView: 2,
          spaceBetween: 0
        },
        320: {
          slidesPerView: 2,
          spaceBetween: 0
        }
      }
    })
  }

  selectPageMenu($event, index) {
    this.setStyle(index)
    this.contentSlides.slideTo(index)
  }
  slideChanged() {
    this.slidesMoving = false
    let index = this.contentSlides.getActiveIndex()
    this.setStyle(index)
    let currentSlide: Element = this.contentSlides._slides[index]
    if (currentSlide) {
      this.slidesHeight = currentSlide.clientHeight
      let contentheight =
        this.screenheight - this.headerheight - this.footerheight - 10
      if (currentSlide.clientHeight > contentheight) {
        this.slidesHeight = currentSlide.clientHeight
      } else {
        this.slidesHeight = contentheight
      }
    }
    this.swiper.slideTo(index, 300)
  }

  slideWillChange() {
    this.slidesMoving = true
  }

  setStyle(index) {
    var slides = document
      .getElementsByClassName('pageMenuSlides')[0]
      .getElementsByClassName('swiper-slide')
    if (index < slides.length) {
      for (var i = 0; i < slides.length; i++) {
        var s = slides[i]
        s.className = 'swiper-slide'
      }
      slides[index].className = 'swiper-slide bottomLine'
    }
  }

  itemSelected(item) {
    this.httpService.requestDataPost(
      'app/inspectionwork/findpointlocation',
      { inspectionWorkNum: item.inspectionWorkNum },
      {},
      (status, datas) => {
        if (status) {
          this.navCtrl.push(InProductionPage, { item, datas })
        }
      }
    )
  }
}
